﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.Store;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;

using System.IO;
using EPocalipse.IFilter;

namespace eBookWinApp
{
  public class LuceneIndexer
  {          
    public LuceneIndexer()
    {
      //DirectoryInfo _indexDir = new DirectoryInfo(indexDir);
      //DirectoryInfo _dataDir = new DirectoryInfo(dataDir);
      //long _start = new Date().getTime();
      //int numIndexed = Index(_indexDir, _dataDir);
      //long end = new Date().getTime();
    }
    
      
    // open an index and start file directory traversal
    public void Index()
    { 
      //state the file location of the index
      string indexFileLocation = @"K:\eBookRoot\LuceneIndex\index"; 
      Lucene.Net.Store.Directory dir = FSDirectory.GetDirectory(indexFileLocation, true);

      //create an analyzer to process the text
      Analyzer analyzer = new SimpleAnalyzer(); 
      
      //create the index writer with the directory and analyzer defined.
      IndexWriter indexWriter = new IndexWriter(dir, analyzer, /*true to create a new index*/ true); 

      //create a document, add in a single field
      Document doc = new Document();
        
      string fileContent = string.Empty;
      string fileName = @"K:\eBookRoot\temp\0\00\[0-00-370333-9] Grammar in Context.pdf";
      //string fileName = @"K:\eBookRoot\LuceneIndex\demo\Lucene.Net.xml";      
      
      using ( TextReader reader = new FilterReader(fileName) )
      {
        fileContent = reader.ReadToEnd().ToLowerInvariant();
      }
      Field fldContent = new Field("content"
                                  , fileContent
                                 //, @"I'm a proud father of my 2 year old son I'm also a software developer and i like to spend my free time playing with and researching technology. I'm also a musician, i like to play some classical piano, some punk rock drums, and some bluesy guitar riffs, never did play much with a bass guitar. thinking about picking up a violin sometime."                                 
                                 , Field.Store.YES
                                 , Field.Index.TOKENIZED
                                 , Field.TermVector.WITH_POSITIONS_OFFSETS );
                               
      doc.Add(fldContent);

        //write the document to the index
        indexWriter.AddDocument(doc);

        //optimize and close the writer
        indexWriter.Optimize(); 
        indexWriter.Close();
      


      
    }

    public string[] Search(string searchItem)
    {
      //state the file location of the index
      string indexFileLocation = @"K:\eBookRoot\LuceneIndex\index";
      Lucene.Net.Store.Directory dir =FSDirectory.GetDirectory(indexFileLocation, false);

      //create an index searcher that will perform the search
      IndexSearcher searcher = new IndexSearcher(dir);

      //build a query object
      Term searchTerm = new Term("content", searchItem);
      Query query     = new TermQuery(searchTerm);

      //execute the query
      Hits hits = searcher.Search(query);

      List<string> result = new List<string>();
      //iterate over the results.
      for (int i = 0; i < hits.Length(); i++)
      {
        Document doc = hits.Doc(i);
        string contentValue = doc.Get("content");
        result.Add(contentValue);

//        Console.WriteLine(contentValue);
      }
      
      return result.ToArray();
      
    }  
      
  }
}
